
const express = require('express');
const fs = require('fs');
const moment = require('moment');
const server = express();
const router = require('./route/all');
server.use('/static',express.static('./resource'));
//全局中间件
server.use(function(req,res,next){

    fs.appendFile('./log.txt',`${moment().subtract(10, 'days').calendar()} ----${req.method} ${req.url} ${req.headers['user-agent']}}\n`,(err,data)=>{})
    next();
})
// 应用导入的路由
server.use('/api',router);
// 全局错误处理中间件
server.use(function(err,req,res,next){

    if(err === 5){
        res.send({
            code:5,
            message:'别看了 token有问题'
        })
    }

    if(err === 4){
        res.send({
            code:4,
            message:'请求方法不对'
        })
    }

    if(err === 2){
        res.send({
            code:2,
            message:'参数数量不够'
        })
    }


    if(err === 3){
        res.send({
            code:3,
            message:'参数格式不对'
        })
    }
});

//启动服务
server.listen(5000,()=>{
    console.log('服务器启动成功');
})